//
//  ServiceViewReplace.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/12/7.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

import WebKit

class ServiceViewReplace: UIViewController,WKUIDelegate,WKScriptMessageHandler,WKNavigationDelegate {
    
    var webView: WKWebView!
    
    var url : String = ""
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let configuration = WKWebViewConfiguration()
        let userContentController = WKUserContentController()

        configuration.userContentController = userContentController
        
        // 设置偏好设置
        let preferences = WKPreferences()
        // 在iOS上默认为NO，表示不能自动通过窗口打开
        preferences.javaScriptCanOpenWindowsAutomatically = true
        preferences.minimumFontSize = 10.0
        configuration.preferences = preferences
        
        configuration.processPool = WKProcessPool()
        webView = WKWebView.init(frame: view.bounds, configuration: configuration)
        
        webView.uiDelegate = self
        webView.navigationDelegate = self
        
        let urlStr = fwqArray.lastObject
        
        webView.load(URLRequest.init(url: URL.init(string: urlStr as! String)!))
        view.addSubview(webView)
    }
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
    }
    
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        
        url = (navigationAction.request.url?.absoluteString)!
        
        if navigationAction.navigationType == WKNavigationType.linkActivated {
            
            //如果为本身或者本身携带#的url进行自身刷新。
            if url == "http://yungou.ie1e.com/app/jlq.aspx" {
                
                //自刷新
                self.webView.load(URLRequest.init(url: URL.init(string: url)!))
                
                fwqArray.removeAllObjects()
                
            } else {
                print(fwqArray)
                
                //取出本地token，进行拼接
                if (localSave.object(forKey: userToken) != nil) {
                    let token = localSave.object(forKey: userToken) as! String
                    
                    if url.contains("?") {
                        url = url + ("&token=") + (token)
                    } else {
                        url = url + ("?&token=") + (token)
                    }
                }
                
                
                fwqArray.add(url)
                aaa(str: fwqArray.lastObject as! String)
            }
            
            decisionHandler(.cancel)
            
        } else {
            decisionHandler(.allow)
            
        }
        
        
    }
    
    //url---
    func aaa(str : String) -> Void {
        
        
        let vvv = ServiceViewController()
        vvv.url = str
        self.navigationController?.pushViewController(vvv, animated: true)
    }
    
    //获取标题
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.navigationItem.title = self.webView.title
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        decisionHandler(.allow)
    }
    
}
